package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianElemeRetailExchangeClient;
import com.diandian.backend.entrance.client.dto.ElemeRetailSkuStockUpdateOneReq;
import com.diandian.backend.entrance.client.dto.eleme.GetSkuStockReqDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 饿了么零售
 */
@Service
public class RouterElemeRetailService {

    private static final Logger logger = LoggerFactory.getLogger(RouterElemeRetailService.class);

    @Autowired
    private DianDianElemeRetailExchangeClient elemeRetailExchangeClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data)  throws Exception {

        // 库存同步
        if(EntranceConstant.ROUTER_ELEME_RETAIL_SYNC_SINGLE_STOCK.equals(cmd)){

            logger.info("执行库存同步请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String source = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_ELEME_RETAIL, EntranceConstant.SETTING_TYPE_ELEME_RETAIL_SOURCE);
            String secret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_ELEME_RETAIL, EntranceConstant.SETTING_TYPE_ELEME_RETAIL_SECRET);

            if(source == null || "".equals(source) || secret == null || "".equals(secret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数不能为空");
            }
            ElemeRetailSkuStockUpdateOneReq syncDTO = JSONObject.parseObject(data.toString(), ElemeRetailSkuStockUpdateOneReq.class);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = elemeRetailExchangeClient.skuStockUpdateOne(source, secret, syncDTO);
            } catch (Exception e){
                logger.error("调用美团闪购服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("库存同步请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        // 查询商品库存数据
        if(EntranceConstant.ROUTER_ELEME_RETAIL_QUERY_SKU_STOCK.equals(cmd)){

            logger.info("执行查询商品库存数据请求，参数: {}", data == null ? "" : data.toJSONString());

            // 获取配置信息
            String source = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_ELEME_RETAIL, EntranceConstant.SETTING_TYPE_ELEME_RETAIL_SOURCE);
            String secret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_ELEME_RETAIL, EntranceConstant.SETTING_TYPE_ELEME_RETAIL_SECRET);

            if(source == null || "".equals(source) || secret == null || "".equals(secret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数不能为空");
            }
            GetSkuStockReqDTO syncDTO = JSONObject.parseObject(data.toString(), GetSkuStockReqDTO.class);

            // 执行请求
            BaseResult resultDTO;
            try{
                resultDTO = elemeRetailExchangeClient.getSkuStock(source, secret, syncDTO);
            } catch (Exception e){
                logger.error("调用饿了么零售服务异常", e);
                resultDTO = new BaseResult(500, "服务访问异常", e.getMessage());
            }

            logger.info("库存查询商品库存数据结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }
}
